##
# In-game interface for the WTF RTS
##

from WtfEngine import *;
import WtfGui;
from Control import *;
import Orders;

class TopBar(WtfGui.LayoutContainer):
	def __init__(self, ctrOpts):
		WtfGui.LayoutContainer.__init__(self, ctrOpts, 0);
		
		self.lblCash = WtfGui.Label("$$ 10000", WtfGui.Font("v_gui_font"));
		self.AddChild(self.lblCash);
		
		self.lblCash.ExpandToText();
		
class Radar(WtfGui.WidgetContainer):
	def __init__(self):
		WtfGui.WidgetContainer.__init__(self, "");
		
		lbl = WtfGui.Label("RADAR HERE", WtfGui.Font("v_gui_font"));
		self.AddChild(WtfGui.LayoutPositioner(lbl, WtfGui.CentrePositioner(), WtfGui.TopPositioner()));
		
		lbl.ExpandToText();
		self.Resize(200, 200);

class Button(WtfGui.ButtonEx):
	def __init__(self, onClick, onRtClick):
		ctrOpts = WtfGui.LayoutContainerCreationOptions();
		
		WtfGui.ButtonEx.__init__(self, ctrOpts);
		
		img = WtfGui.Image(TextureRef("media/images/RtsButtons/stopOrder.png"));
		img.Resize(48, 48);
		self.AddChild(img);
		
		self.onPress = Function(onClick);
		if(onRtClick != 0):
			self.onRightClick = Function(onRtClick);
		
		
#
# The actual interface. This also encapsulates the game state (by the window manager)
#
class Interface(WtfGui.ScreenOverlay):
	def __init__(self):
		
		WtfGui.ScreenOverlay.__init__(self);
		
		ctrOpts = WtfGui.LayoutContainerCreationOptions();
		
		# Create bars
		self.topBar = TopBar(ctrOpts);
		self.leftBar = WtfGui.LayoutContainer(ctrOpts, 1);
		self.rightBar = WtfGui.LayoutContainer(ctrOpts, 1);
		self.bottomBar = WtfGui.LayoutContainer(ctrOpts, 0);
		
		# Radar
		self.radar = Radar();
		
		# Add elements
		self.AddChild(WtfGui.LayoutPositioner(self.topBar, WtfGui.CentrePositioner(), WtfGui.TopPositioner()));
		self.AddChild(WtfGui.LayoutPositioner(self.leftBar, WtfGui.LeftPositioner(), WtfGui.CentrePositioner()));
		self.AddChild(WtfGui.LayoutPositioner(self.rightBar, WtfGui.RightPositioner(), WtfGui.CentrePositioner()));
		self.AddChild(WtfGui.LayoutPositioner(self.bottomBar, WtfGui.CentrePositioner(), WtfGui.BottomPositioner()));
		
		self.AddChild(WtfGui.LayoutPositioner(self.radar, WtfGui.LeftPositioner(), WtfGui.BottomPositioner()));

		# Listen to selection changes
		Controller.GetInst().Listen(self.OnSelectionChange);
		
	def OnSelectionChange(self):
		self.leftBar.Clear();
		self.bottomBar.Clear();
		map(self.AddOrder, Controller.GetInst().orders);
		
	def AddOrder(self, order):
		if(order.button == None):
			order.button = Button(Function(lambda: Controller.GetInst().InvokeOrder(order)), 0);
		if(isinstance(order, Orders.BuildOrder)):
			self.bottomBar.AddChild(order.button);
		else:
			self.leftBar.AddChild(order.button);

class GameMenu(WtfGui.WindowManager):
	def __init__(self):
		
		WtfGui.WindowManager.__init__(self);
		
		ctrOpts = WtfGui.LayoutContainerCreationOptions();
		
		# Create menu
		font = WtfGui.Font("v_gui_font");

		self.menu = WtfGui.Menu("Game Menu");
		self.menu.Resize(300, 100);

		self.menu.AddChild(WtfGui.Button(Function
			(lambda e: self.WmToggle()), "Resume Game", font));
		self.menu.AddChild(WtfGui.Button(Function
			(lambda e: gWm.AddWindow(Options(font), 1)), "Options", font));
		self.menu.AddChild(WtfGui.Button(Function
			(lambda e: self.SpawnConsole()), "Developer's Console", font));
		self.menu.AddChild(WtfGui.Button(Function
			(lambda e: self.AddWindow(WtfGui.SceneDiagnostics(), 1)), "Diagnostics", font));
		self.menu.AddChild(WtfGui.Button(Function(lambda e: exit()), "Exit", font));
		
		# Add elements
		self.AddChild(WtfGui.LayoutPositioner(self.menu, WtfGui.CentrePositioner(), WtfGui.CentrePositioner()));
		